/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <mc/log.h>

#include "hi183x_om.h"

namespace dev {
bool hi183x_optical_module::start() {
    return true;
}

bool hi183x_optical_module::stop() {
    return true;
}
// 实现optical_module的虚函数
bool hi183x_optical_module::init(mc::mutable_dict& config, const mc::dict& connector) {
    try {
        from_variant(config, *this);
    } catch (const std::exception& e) {
        elog("hi183x_optical_module init failed, exception: ${exception}", ("exception", e.what()));
        return false;
    }
    return true;
}

bool hi183x_optical_module::reset_default_properties() {
    m_presence                                                  = false;
    m_presence_debounce                                         = 0;
    m_optical_module.Manufacturer                               = "";
    m_optical_module.PartNumber                                 = "";
    m_optical_module.SerialNumber                               = "";
    m_optical_module.MediumType                                 = "";
    m_optical_module.ConnectorType                              = "";
    m_optical_module.TransmissionDistance                       = 0xFFFF;
    m_optical_module.WaveLengthNanometer                        = "";
    m_optical_module_status.RXLossState                         = false;
    m_optical_module_status.TXLossState                         = false;
    m_optical_module_status.TXFaultState                        = false;
    m_optical_module.Identifier                                 = "";
    m_optical_module_current.TXBiasCurrentMilliAmps             = std::vector<double>(0);
    m_optical_module_power.TXOutputPowerMilliWatts              = std::vector<double>(0);
    m_optical_module_power.RXInputPowerMilliWatts               = std::vector<double>(0);
    m_optical_module_voltage.SupplyVoltage                      = 0xFFFF;
    m_optical_module_power.TXPowerUpperThresholdCritical        = 0xFFFF;
    m_optical_module_power.TXPowerLowerThresholdCritical        = 0xFFFF;
    m_optical_module_voltage.VoltageUpperThresholdCritical      = 0xFFFF;
    m_optical_module_voltage.VoltageLowerThresholdCritical      = 0xFFFF;
    m_optical_module_power.RXPowerUpperThresholdCritical        = 0xFFFF;
    m_optical_module_power.RXPowerLowerThresholdCritical        = 0xFFFF;
    m_optical_module_current.TXBiasCurrentUpperThresholdCritical = 0xFFFF;
    m_optical_module_current.TXBiasCurrentLowerThresholdCritical = 0xFFFF;

    return true;
}

bool hi183x_optical_module::start_ncsi_protocol(ncsi_over_mctp_hw_ptr ncsi_over_mctp,
                                                mc::milliseconds      interval) {
    uint8_t port_id = m_optical_module.get_port_id();
    m_optical_module.start_ncsi_update_task_huawei(ncsi_over_mctp, mc::milliseconds(10000));
    m_optical_module_cooling.start_ncsi_update_task_huawei(ncsi_over_mctp, interval,
                                                           port_id);
    m_optical_module_status.start_ncsi_update_task_huawei(ncsi_over_mctp, mc::milliseconds(10000),
                                                          port_id);
    m_optical_module_power.start_ncsi_update_task_huawei(ncsi_over_mctp, mc::milliseconds(10000),
                                                         port_id);
    m_optical_module_voltage.start_ncsi_update_task_huawei(ncsi_over_mctp, mc::milliseconds(10000),
                                                           port_id);
    m_optical_module_current.start_ncsi_update_task_huawei(ncsi_over_mctp, mc::milliseconds(10000),
                                                           port_id);

    return true;
}
} // namespace dev

MC_REFLECT(dev::hi183x_optical_module,
           ((m_optical_module, "bmc.dev.OpticalModule"))((m_optical_module_status,
                                                          "bmc.dev.OpticalModule.Status"))(
               (m_optical_module_cooling, "bmc.dev.OpticalModule.Cooling"))(
               (m_optical_module_voltage, "bmc.dev.OpticalModule.Voltage"))(
               (m_optical_module_power, "bmc.dev.OpticalModule.Power"))(
               (m_optical_module_current, "bmc.dev.OpticalModule.Current")))